<?php

class ReimburseModel extends Model {

    function __construct(){
    	$this->db = parent::dbConnect($GLOBALS ["gDataBase"] ["db"]);
    }

    function getItems($con,$tab,$pageCount){
    	/**
    	 * 实现查找与分页的功能
    	 * @param array $con 查询条件
    	 * @param string $tab 查询的表名
    	 * @param int $pageCount 每页显示的记录数
    	 */

    	 $select = $this->db->select();
    	 $select->from(" ".$tab." ");

    	 if(isset($con['a_title']) && !empty($con['a_title'])) $select->where(" a_title like '%".$con['a_title']."%'");
    	 if(isset($con['a_sdate']) && !empty($con['a_sdate'])) $select->where(" a_sdate>='".$con['a_sdate']."'");
    	 if(isset($con['a_edate']) && !empty($con['a_edate'])) $select->where(" a_edate<='".$con['a_edate']."'");
    	 if(isset($con['a_auditbill'])) $select->where(" a_auditbill={$con['a_auditbill']}");
    	 if(isset($con['need_express'])) $select->where(" need_express={$con['need_express']}");
    	 if(isset($con['a_isreim'])) $select->where(" a_isreim={$con['a_isreim']}");
    	 if(isset($con['b_name']) && !empty($con['b_name'])) $select->where(" b_name like '%".$con['b_name']."%'");
    	 if(isset($con['user_realname']) && !empty($con['user_realname'])) $select->where(" user_realname like '%".$con['user_realname']."%'");
    	 if(isset($con['recv_extend'])) $select->where(" recv_extend!=0");
    	 if(isset($con['is_received'])) $select->where(" is_received=1");
    	 if(isset($con['exp_isreim'])) $select->where(" exp_isreim=0");
    	 if(isset($con['app_realname']) && !empty($con['app_realname'])) $select->where(" app_realname like '%".$con['app_realname']."%'");

    	 $total = $select->count();
    	 $list = array();

    	//创建分页
    	$pageCount = 10;
    	include_once 'Pager.class.php';
    	$list['page'] = new Pager($total, $pageCount);//创建分页对象
    	$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

		$select->limit ( $list['page']->offset(), $pageCount );
		$rs = $select->query();
		if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;

    }

  function getReimAssign(){
    	//获取需报销的任务（已通过发票审批、无需叫快递，并且未报销）

    	$reimassigns = $this->db->getAll("select a_id,a_title,a_auditbill,b_id,cs_id,user_id,a_cost from assignment a where a.a_auditbill=1 and a.need_express=0 and a.a_isreim=0");
    	$total = count($reimassigns);

    	$list = array();

    	//创建分页
    	$pageCount = 10;
    	include_once 'Pager.class.php';
    	$list['page'] = new Pager($total, $pageCount);//创建分页对象
    	$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select a_id,a_title,a_auditbill,b_id,cs_id,user_id,a_cost from assignment a where a.a_auditbill=1 and a.need_express=0 and a.a_isreim=0 limit ".$list['page']->offset().", ".$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';
    	return (array) $list;
    }


    function getReimExpress(){
    	//return $this->db->getAll("select * from express e where e.recv_extend!=0");
    	//return $this->db->getAll("select * from express");//返回二维数组
    	//取得需报销的快递（有补汇金额、到付快递已到且未报销）
    	$reimexpres = $this->db->getAll("select * from express e where e.recv_extend != 0 and e.is_received = 1 and e.exp_isreim = 0 ");
    	$total = count($reimexpres);
    	$list = array();
    	$pageCount = 10;

    	//新建分页对象
    	include_once "Pager.class.php";
    	$list['page'] = new Pager($total, $pageCount);
    	$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from express e where e.recv_extend != 0 and e.is_received = 1 and e.exp_isreim = 0 limit ".$list['page']->offset().', '.$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';
    	return (array) $list;
    }

    function getBrandTitle($bid){
    	return $this->db->getRow("select b_name from brand where b_id=$bid");//返回一维数组
    }

    function getUserPay($user_id){
    	return $this->db->getRow("select realname,bank_realname,bank_account,alipay,alipayrealname from user_ext where user_id=$user_id");
    }

    function getStoreReim($cs_id){
    	return $this->db->getRow("select cs_reim from store where cs_id=$cs_id");
    }

    function getAssignTitle($a_id){
    	return $this->db->getRow("select a_title,a_cost from assignment where a_id=$a_id");
    }

    function saveReimburse($a_id,$b_name,$a_title,$user_realname,$user_account,$store_reim,$meal_cost,$shouldreim,$acctuallyreim,$reim_date,$reim_way,$is_express){
    	return $this->db->execute("insert into reimbursement (a_id,b_name,a_title,user_realname,user_account,store_reim,meal_cost,shouldreim,is_auditbill,is_express,is_reim,reim_actually,reim_date,reim_way)".
    	                   " values($a_id,'$b_name','$a_title','$user_realname','$user_account',$store_reim,$meal_cost,$shouldreim,1,$is_express,1,$acctuallyreim,'$reim_date','$reim_way')");
    }

    function updateAssign($a_id){
    	return $this->db->execute("update assignment set a_isreim=1 where a_id=$a_id");
    }

    function updateExpAssign($ex_id){
    	return $this->db->execute("update express set exp_isreim=1 where ex_id=$ex_id");
    }

    function getBrandId($a_id){
    	return $this->db->getOne("select b_id from assignment where a_id=$a_id ");
    }

    function getAssignInfo($a_id){
    	return $this->db->getRow("select a_auditbill from assignment where a_id=$a_id ");
    }

    function getAllReims(){
    	$allreims = $this->db->getAll("select * from reimbursement");
    	$total = count($allreims);

    	//新建分页对象
    	$pageCount = 10;
    	$list = array();
    	include_once "Pager.class.php";
    	$list['page'] = new Pager($total, $pageCount);
    	$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from reimbursement limit ".$list['page']->offset().', '.$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';
    	return (array) $list;
    }

    function getItemsWithNopage($con,$table){
		/**
		 * 实现不分页的信息查找
		 */
		 $select = $this->db->select();
		 $select->from(" $table ");

		 if(isset($con['a_sdate']) && !empty($con['a_sdate'])) $select->where(" a_sdate>='".$con['a_sdate']."'");
    	 if(isset($con['a_edate']) && !empty($con['a_edate'])) $select->where(" a_edate<='".$con['a_edate']."'");

    	 $list = array();
    	 $list = $select->query();

    	 return $list;
	}
}
?>